//引入http模块
const http = require('http')

//创建一个http实例
const server = http.createServer()

//监听request事件
server.on('request', (req, res) => {
  //根据不同的url地址返回不同的内容
  console.log(req.url)

  //设置响应头
  res.setHeader('Content-Type', 'text/html; charset=utf-8')

  if (req.url === '/index.html') {
    //返回内容
    res.end('<h2>我是主页</h2>')
  } else if (req.url === '/login.html' || req.url === '/') {
    //返回内容
    res.end('<h2>我是登录页</h2>')
  } else {
    //返回内容
    res.end('<h2>404 NOT FOUND</h2>')
  }
})

//启动服务
server.listen('8081', () => {
  console.log('My server is running in http://localhost:8081')
})
